//漫画章节
import 'package:path/path.dart' as path;
import 'package:xvideos/config/address.dart' as address;
import 'package:xvideos/hj_model/cartoon_media_info.dart';
import 'package:xvideos/tools_base/global_store/store.dart';

import '../media_content.dart';

class ComicChapterInfo {
  int? episodeNumber;
  bool? hasBuy;
  String? id;
  int? listenPermission; //  0:会员 1:金币购买 2:免费
  String? mediaId;
  String? name;
  int? price;
  String? createdAt;
  String? cover;
  String? audioUrl;

  MediaContent? mediaContent; //（另外的接口数据返回，外部手动赋值）

  // 有声小说
  String get getRealAudioUrl {
    if (audioUrl == null || audioUrl!.isEmpty) {
      return "";
    }
    if (!audioUrl!.startsWith("http") && !audioUrl!.startsWith("https")) {
      return path.join(address.Address.audioCdnAddress ?? '', audioUrl);
    }
    return audioUrl!;
  }

  ComicChapterInfo({
    this.episodeNumber,
    this.hasBuy,
    this.id,
    this.listenPermission,
    this.mediaId,
    this.name,
    this.price,
    this.createdAt,
    this.cover,
    this.audioUrl,
  });

  ComicChapterInfo.fromJson(Map<String, dynamic> json) {
    episodeNumber = json['episodeNumber'];
    hasBuy = json['hasBuy'];
    id = json['id'];
    listenPermission = json['listenPermission'];
    mediaId = json['mediaId'];
    name = json['name'];
    price = json['price'];
    createdAt = json['createdAt'];
    cover = json['cover'];
    audioUrl = json['audioUrl'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['episodeNumber'] = this.episodeNumber;
    data['hasBuy'] = this.hasBuy;
    data['id'] = this.id;
    data['listenPermission'] = this.listenPermission;
    data['mediaId'] = this.mediaId;
    data['name'] = this.name;
    data['price'] = this.price;
    return data;
  }

  //是否能播放
  bool get canPlay {
    //1. 是否是免费
    if (listenPermission == 2) return true;
    //2. 金币是否是否购买
    if (listenPermission == 1 && (hasBuy ?? false)) return true;
    //3. 会员视频，是否是会员
    if (listenPermission == 0 && globalStore.isACGVIP()) return true;
    return false;
  }

  CartoonMediaInfo toCartoonMediaInfo() {
    CartoonMediaInfo? mediaInfo = CartoonMediaInfo();
    mediaInfo.title = name;
    mediaInfo.price = price;
    mediaInfo.permission = listenPermission;
    mediaInfo.id = id;
    mediaInfo.mId = mediaId;
    mediaInfo.createdAt = createdAt;
    mediaInfo.verticalCover = cover;
    mediaInfo.horizontalCover = cover;
    mediaInfo.mediaType = 'image';
    mediaInfo.episodeNumber = episodeNumber;
    // mediaInfo.audioUrl = audioUrl;
    return mediaInfo;
  }
}
